<style>
    .boldRow{
        font-weigh&nbsp&nbsp&nbspT: bolder;
        
    }
</style>
<script>
    $(document).ready(function(){
        $('.stateTotalSign').parent().parent().addClass('boldRow');
    });  
</script>


    

<center><h2>QSI, INC</h2>
<h1>Earned / Un-Earned Contract Report</h1>
<h3>Start Date: <?php echo DateHelper::convertDate('m-d-Y',$model->startDate); ?> End Date <?php echo DateHelper::convertDate('m-d-Y',$model->endDate); ?></h3>
</center>
<?php
$data = $model->getReportData();
$itemTable = new CI_Table();
$itemTable->set_template(array(
    'table_open'=>'<table width="100%">'
));
$itemTable->set_heading('St', 'Contract No', 'Type', 'Date', 'No', 'County', 'Custno-Locno-Locname', 'Amount', 'Tax');
$itemTable->add_row(array(
    'data'=>'<hr/>',
    'colspan'=>9
));
$grandTable = new CI_Table();
$grandTable->set_template(array(
    'table_open'=>'<table width="100%" style="font-weigh&nbsp&nbsp&nbspT:bold;">'
));
if ($data!==false && count($data) > 0)
{    
    foreach ($data as $stateName => $stateData)
    {
        $contracts = $stateData['contracts'];
        $itemTable->add_row("<b>$stateName</b>");
        
        foreach($contracts as $contract){
            $itemTable->add_row(
                    '',
                    array(
                        'data'=>$contract['contractid'],
                        'colspan'=>1
                    ),
                    array(
                        'data'=>  ContractStatus::getText($contract['status'])
                    ),
                    array(
                        'data'=>'Start: '.DateHelper::convertDate('m-d-Y',$contract['contractstartdate']),
                        'colspan'=>2
                    ),
                    array(
                        'data'=>'End: '.DateHelper::convertDate('m-d-Y',$contract['contractenddate']),
                        'colspan'=>2
                    ),
                    array(
                        'data'=>  ContractType::getText($contract['billtype'])
                    ),
                    array(
                        'data'=>'No. of Earned Months:'.$contract['earnMonths'],
                        'colspan'=>1
                    )
             );
            $itemTable->add_row(
                    '',
                    array(
                        'data'=>'<hr/>',
                        'colspan'=>8
                    )
            );
            $invoices = $contract['items'];
            if(count($invoices)){
                foreach($invoices as $invoice){
                    $itemTable->add_row(
                            '',
                            '',
                            $invoice['invoicetype'],
                            DateHelper::convertDate('m-d-Y',$invoice['invoicedate']),
                            $invoice['invoice'],
                            $invoice['county'],
                            $invoice['custno'].' - '.$invoice['locno'].' - '.$invoice['locname'],
                            array(
                                'data'=>$invoice['amountcharge'],
                                'align'=>'right'
                            ),
                            array(
                                'data'=>$invoice['tax'],
                                'align'=>'right'
                            )
                    );
                }                
            }else{
                $itemTable->add_row(
                        '',
                        array(
                            'data'=>'No Billing for this Contract',
                            'colspan'=>6
                        ),
                        array(
                            'data'=>'0.00',
                            'align'=>'right'
                        ),
                        array(
                            'data'=>'0.00',
                            'align'=>'right'
                        )
                );
            }
            $itemTable->add_row(
                        '','','','','','','',array(
                            'data'=>'<hr/>',
                            'colspan'=>2
                        ));
            $itemTable->add_row(
                    '','','','','','','',
                    array(
                        'data'=>number_format($contract['totalAmount'],2),                            
                        'align'=>'right'
                    ),
                    array(
                        'data'=>number_format($contract['totalTax'],2),                            
                        'align'=>'right'
                    ));
            $itemTable->add_row(
                    '','',
                    array(
                        'data'=>'Earned: I:'.number_format($contract['earn'],2).' &nbsp&nbsp&nbspT:'.number_format($contract['earnTax'],2),
                        'colspan'=>3,
                        'align'=>'right'
                    ),
                    array(
                        'data'=>'Un-Earned: '.number_format($contract['unearn'],2).' &nbsp&nbsp&nbspT:'.number_format($contract['unearnTax'],2),
                        'colspan'=>2,
                        'align'=>'right'
                    )
                    );
            $itemTable->add_row(array(
                'data'=>'<hr/>',
                'colspan'=>9
            ));
            
        }
        
        //state total row
        $itemTable->add_row(
                    array(
                        'data'=>$stateName.' Total'.'<span class="stateTotalSign"></span>',
                        'colspan'=>2
                    ),
                    array(
                        'data'=>'Earned: I:'.number_format($stateData['earn'],2).' &nbsp&nbsp&nbspT:'.number_format($stateData['earnTax'],2),
                        'colspan'=>3,
                        'align'=>'right'
                    ),
                    array(
                        'data'=>'Un-Earned: I:'.number_format($stateData['unearn'],2).' &nbsp&nbsp&nbspT:'.number_format($stateData['unearnTax'],2),
                        'colspan'=>2,
                        'align'=>'right'
                    ),
                    array(
                        'data'=>number_format($stateData['totalAmount'],2),
                        'align'=>'right'
                    ),
                    array(
                        'data'=>number_format($stateData['tax'],2),
                        'align'=>'right'
                    )
                    );
        $itemTable->add_row(array(
                'data'=>'<hr/>',
                'colspan'=>9
            ));
        
    }
    
    
    $grandTable->add_row('Summary');
    $grandEarn = 0;
    $grandEarnTax=0;
  
    $grandUnearn = 0;
    $grandUnearnTax = 0;
    $grandAmount = 0;
    $grandTax = 0;
    foreach($data as $stateName=>$stateData){
        $grandTable->add_row(array(
            array(
                'data'=>$stateName,
                'colspan'=>2
            ),
            array(
                'data'=>'Earned: I:'.number_format($stateData['earn'],2).' &nbsp&nbsp&nbspT:'.number_format($stateData['earnTax'],2),
                'colspan'=>3,
                'align'=>'right'
            ),
            array(
                'data'=>'Un-Earned: I:'.number_format($stateData['unearn'],2).' &nbsp&nbsp&nbspT:'.number_format($stateData['unearnTax'],2),
                'colspan'=>2,
                'align'=>'right'
            ),
            array(
                'data'=>number_format($stateData['totalAmount'],2),
                'align'=>'right'
            ),
            array(
                'data'=>number_format($stateData['tax'],2),
                'align'=>'right'
            )
        ));
        $grandEarn+=$stateData['earn'];
        $grandEarnTax +=$stateData['earnTax'];        
        $grandUnearn+=$stateData['unearn'];
        $grandUnearnTax+=$stateData['unearnTax'];
        $grandAmount +=$stateData['totalAmount'];
        $grandTax+=$stateData['tax'];
    }
    $grandTable->add_row('<br/>');
    $grandTable->add_row(array(
            array(
                'data'=>'Grand Total',
                'colspan'=>2
            ),
            array(
                'data'=>'Earned: I:'.number_format($grandEarn,2).' &nbsp&nbsp&nbspT:'.number_format($grandEarnTax,2),
                'colspan'=>3,
                'align'=>'right'
            ),
            array(
                'data'=>'Un-Earned: '.number_format($grandUnearn,2).' &nbsp&nbsp&nbspT:'.number_format($grandUnearnTax,2),
                'colspan'=>2,
                'align'=>'right'
            ),
            array(
                'data'=>number_format($grandAmount,2),
                'align'=>'right'
            ),
            array(
                'data'=>number_format($grandTax,2),
                'align'=>'right'
            )
        ));
}
echo $itemTable->generate();
echo '<br/>';
echo $grandTable->generate();
?>